#!/bin/bash
#Any problem, please contact with djx.zhenghua@gmail.com
args=$@
ioption=0
roption=0
foption=0
listoption=0
optargs=""
trashfolder=${TRASHFOLDER:-/tmp/$USER-trash/}
mvlogf=$trashfolder/.log
RM=/bin/rm
########################### define function start ###########################
rmhelp(){
	echo Usage: rm [OPTION]... FILE... 
	echo "    -f"
	echo "    -c empty the trash bin"
	echo "    -d delete the file forever"
	echo "    -u recover the file (unrm)"
    echo "    -l list the file in trash bin"
	echo "  example"
	echo "     rm file0 file1      : move the file file0 file1 to trash bin"
	echo "     rm -u file0 file1   : recover the file file0 file1"
	echo "     rm -d file0         : delete the file forever, it will can not be recoverd"
	echo "     rm -c               : empty the trash bin"
	echo "     You can check the log file $trashfolder/.log"
	echo "     To use rm -u, you can do "
	echo "		  alias unrm=\"rm -u\""
	echo "     To use rm -d, you can do "
	echo "		  alias rmd=\"rm -d\""
}

cleanAll(){
	$RM -rf $trashfolder
}

recover(){
	for i in "$@" ;do
		if [ "-" != "${i:0:1}" ]; then
			if [ "/" != "${i:0:1}" ]; then
				mvsf=$PWD/$i
			else
				mvsf=$i
			fi
			mvdf=$trashfolder/$(echo $mvsf | tr "/" "*" )
			if [ -e $mvdf ]; then
				mv  $mvdf $mvsf
				if [ 0 -eq $? ]; then
					echo "restore $mvdf		$mvsf" >> $mvlogf
				fi
			else
				echo $mvsf dose not been stored
			fi
		fi
	done	
}

listBin(){
	if [ -e "$trashfolder" ]; then
    cd $trashfolder
    for i in * ; do
        fn=$(echo $i | tr "*" "/" )
        echo $fn
    done
    fi
}
########################### define function end  #############################

for i in "$@" ;do
	if [ "-" = "${i:0:1}" ]; then
		optargs="$optargs $i"
	fi
done

while getopts 'hiIfrvlcud' OPT $optargs ;do
	case $OPT in
	f)
		foption=1;;
	d)
		/bin/rm $*
		exit 0;;
	v)
		voption=1;;
    l)
        loption=1;
        listBin;
        exit 0;;
	r)
		roption=1;;
	i)
		ioption=1;;
	I)
		Ioption=1;;
	u)
		recover $*;
		exit 0;;
	c)
		cleanAll; exit 0;;
	h)
		rmhelp
		exit 0;;
	esac	
done

if [ ! -e $trashfolder ]; then
	mkdir -p $trashfolder
fi

for i in "$@" ;do
	if [ "-" != "${i:0:1}" ]; then
		#if [ -d $i ]; then
			#echo $i is folder
		#elif [ -e $i ]; then
			#echo $i is file
		#fi

        #if the $i is "   "
        tmpfn=`echo $i`
		if [ "$tmpfn" = "" ]; then
            continue
        fi

		if [ "/" != "${i:0:1}" ]; then
			mvsf=$PWD/$i
		else
			mvsf=$i
		fi
		basedf=$(echo $mvsf |tr "/" "*" )
		#basedf=$(basename $i)
		if [ 0 -ne $? ]; then
            continue
        fi
		mvdf=$trashfolder/$basedf
		if [ -e "$mvsf" ]; then
			if [ -e "$mvdf" ];then
				${RM} -rf "$mvdf"
				mv -f "$mvsf" "$mvdf"
			else
				mv "$mvsf" "$mvdf"
			fi
		else
			if [ $foption != 1 ]; then
				echo "rm: cannot remove $mvsf: No such file or directory"
			fi
		fi 
		if [ 0 -eq $? ]; then
			echo "store   $mvsf		$mvdf" >> $mvlogf
		fi
	fi
done

#cleanHalf(){
#	cmdclean=/tmp/cmdclean
#	awk -F";" '{print $2}' $mvlogf > $cmdclean
#	chmod +x $cmdclean
#	$cmdclean
#	$RM $cmdclean
#	$RM $mvlogf
#}
echo 0 >/dev/null
